// RTSPClientAdapter.h: interface for the CRTSPClientAdapter class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_RTSPCLIENTADAPTER_H__AFCD1810_9DDA_4E6E_A32C_7B5152F301B4__INCLUDED_)
#define AFX_RTSPCLIENTADAPTER_H__AFCD1810_9DDA_4E6E_A32C_7B5152F301B4__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#ifdef __Win32__
#pragma warning(disable:4786)
#endif

#include <string>
#include <list>
#include <map>

#include "RTSPClient.h"
#include "OSThread.h"
#include "SdpParser.h"
#include "RTPBuffer.h"
#include "CommonOpt.h"
#include "TimeSlice.h"
#include "BusinessDNA.h"
#include "LiveRecvFilter.h"

typedef struct _RTPHEADER
{
	UInt16	Flag;
	UInt16	SeqNo;
	UInt32	TimeStamp;
}RTPHEADER;

using namespace std;

class CTimeBuffer;

class CRTSPClientAdapter : public OSTask  
{
	struct CSDPINFO
	{
		UInt32 m_TrackID;
		UInt32 m_TimeScale;
		CRtpSortContainer  m_sortBuf;
	};
	
	struct InterMsg 
	{
		UInt32	Type;
		UInt32  TrackID;
		Int32	Ret;
	};
	
public:
	CRTSPClientAdapter(const string& strPrgmName, const CBusinessDNA &inBusinessParer);
	virtual ~CRTSPClientAdapter();
	Bool Initialize(CTimeBuffer *pBuffer, string strIP ="" , UInt16 nPort =0 , CRTSPClient::EProtocol eProto=CRTSPClient::CTUDP);
	Bool Run();

	
	int	Start();
	int	Stop();
	
	int	Connect();
	int	Setup();
	int	Play(float vStartTime);
	int	Pause();
	int	Close();
	float GetRange();
	
	const char * GetSDP();

	void EnAutoStepForwardPT() { m_willAutoStepForward = TRUE; }
	
	Int32 GetID() {return m_OwnerID;}

	OS_Error AddSliceToTimeBuffer(CTimeSlice* pTimeSlice);
	
protected:	
	Bool DoMsg(UInt32 vType,UInt32 TrackID);
	Bool Teardown();
	OS_Error AddRtpPacketToTimebuffer( RtpPacket* pRtpPacket,UInt32 vTrackID=0);

	void	MoveForwardplayingPoint(UInt32 nInterval);
		
	void	EchoInfo2UI();

protected:
	string			m_strPrgmName;

	CBusinessDNA	m_cBusinessDNA;

	CLiveRecvFilter*        m_pRecvFilter;

	CRTSPClient		m_RTSPClient;
	CTimeBuffer*	m_pTimeBuffer;


	Bool			m_willAutoStepForward;
	Bool			m_isStartStepForwardPT;
	
	CTimeSlice*      m_pSlice;
	
	Bool			m_hasSetStartTime;
	OSMutex			m_mtxHasSetStartTime;
	OSCond			m_conHasSetStartTime;

	//Parameters
	UInt32			m_IP;
	UInt16			m_Port;
	
	Bool			m_IsPlaying;
	
	Int32			m_RTSPStatus;  
	
	//
	std::list< InterMsg *> m_MsgList;
	OSCond				m_BlockCond;
	OSMutex				m_BlockMutex;
	
	static Int32		m_ID;
	Int32				m_OwnerID;
	
	OSMutex		m_IsRunMutex;
	Bool		m_IsRun;
	
	char		m_ErrorMsg[100];
	Int32		m_ErrorCode;
	
	//update Seqno to check if seqno is order
	std::map<UInt32,UInt16>	m_SeqNoList;

	CSdpParser m_SdpParser;

	TrackMap	m_TrackList;

	UInt64		m_lastEchoTime;

	Bool		m_bSetupConvert;

	//key is payload type(video,audio,other)
	std::map<UInt8, UInt32>		m_TrackTypes;

#ifdef DEBUG
	UInt64		m_nLastPrintBMTime;
#endif
};

#endif // !defined(AFX_RTSPCLIENTADAPTER_H__AFCD1810_9DDA_4E6E_A32C_7B5152F301B4__INCLUDED_)
